<?php

namespace app\daren\model\job;

use app\daren\model\daren\Daren;
use app\operations\model\Store;
use sheep\model\Common;
use think\Model;
use think\model\concern\SoftDelete;

class ProbeJob extends Common
{
    use SoftDelete;

    protected $name = 'field_probe_job';

    protected $type = [
        'activity_time' => 'timestamp:Y-m-d',
        'start_time' => 'timestamp:Y-m-d H:i:s',
        'end_time' => 'timestamp:Y-m-d',
        'receiving_level' => 'json'
    ];

    public function getStatusAttr($value)
    {
        $status = [
            1 => '未审核',
            2 => '已通过',
            3 => '已拒绝',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    public function getTaskStatusAttr($value)
    {
        $task = [
            1 => '未开始',
            2 => '进行中',
            3 => '结束',
        ];
        return ['text' => $task[$value], 'value' => $value];
    }

    public function getShelfStatusAttr($value)
    {
        $status = [
            1 => '上架',
            2 => '下架',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 达人审核列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param = [])
    {
        $model = $this;
        $where = [];
        // 探店分类
        if (!empty($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }
        // 名称
        if (!empty($param['title'])) {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }
        // 任务状态
        if (!empty($param['task_status'])) {
            $where[] = ['task_status', '=', $param['task_status']];
        }
        // 上架状态
        if (!empty($param['shelf_status'])) {
            $where[] = ['shelf_status', '=', $param['shelf_status']];
        }

        // 创建时间
        if (!empty($param['start_time'])) {
            $where[] = ['create_time', '>', strtotime($param['start_time'])];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['create_time', '<=', strtotime($param['end_time'])];
        }
        if (!empty($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }

        return $model->with('store')->order('id desc')->where($where)->paginate($param);
    }

    /**
     * 添加
     * @param $data
     * @return NewJob
     */
    public function dataCreate($data)
    {
        return $this->create($data);
    }

    /**
     * 修改
     * @param $data
     * @return NewJob
     */
    public function dataEdit($data)
    {
        return self::update($data);
    }

    /**
     * 详情
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function detail($id)
    {
        return $this->with('store')->where('id', $id)->findOrEmpty();
    }

    /**
     * 审核状态修改
     * @param $data
     * @return Daren|Model
     */
    public function dataStatusEdit($data)
    {
        $darenModel = self::find($data['id']);
        if (!$darenModel) {
            return error('数据不存在');
        }
        $darenModel->status = $data['status'];
        $darenModel->remark = $data['remark'];
        $darenModel->save();
        return $darenModel;
    }

    /**
     * 上下架修改
     * @param $data
     * @return Daren|Model
     */
    public function shelfStatusEdit($data)
    {
        $darenModel = self::find($data['id']);
        if (!$darenModel) {
            return error('数据不存在');
        }
        $darenModel->shelf_status = $data['shelf_status'];
        if ($data['shelf_status'] == 1) {
            $darenModel->start_time = time();
        }
        $darenModel->save();
        return $darenModel;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function deleteData($id)
    {
        return self::destroy($id);
    }

    public function store()
    {
        return $this->hasOne(Store::class, 'id', 'store_id');
    }


}